package com.smartleanx.module.hrm.controller.admin.dept;

import cn.hutool.core.collection.CollUtil;
import com.smartleanx.framework.common.pojo.CommonResult;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeDO;
import com.smartleanx.module.hrm.intergration.PostLevelServiceProvider;
import com.smartleanx.module.hrm.service.employee.EmployeeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;

import static com.smartleanx.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.smartleanx.framework.common.pojo.CommonResult.success;
import static com.smartleanx.module.hrm.enums.ErrorCodeConstants.POST_LEVEL_EXISTS_EMPLOYEE;


@Tag(name = "管理后台 - 劳安心职级")
@RestController
@RequestMapping("/hrm/post-level")
@Validated
public class PostLevelController {

    @Resource
    private PostLevelServiceProvider postLevelServiceProvider;

    @Resource
    private EmployeeService employeeService;

    @DeleteMapping("/delete")
    @Operation(summary = "删除岗级")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:post-level:delete')")
    public CommonResult<Boolean> deletePostLevel(@RequestParam("id") Long id) {
        List<EmployeeDO> empList = employeeService.getEmployeeListByPostLevelId(id);
        if (CollUtil.isNotEmpty(empList)) {
            throw exception(POST_LEVEL_EXISTS_EMPLOYEE);
        }
        postLevelServiceProvider.deletePostLevel(id);
        return success(true);
    }
}
